/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.GridLayout;
import org.aswing.JCheckBox;
import org.aswing.JPanel;

import com.bourre.data.collections.IndexedArray;

import fever.data.validators.ValidationResources;
import fever.utils.StringUtil;

import fvaswing.components.forms.FvFormField;

/**
 * Poll group ( using <strong>checkbox</strong> ) as form field.
 * 
 * <p>Allow user to place a group of checkbox as unique field into form.<br />
 * 
 * <p>Choices must be added to field before adding field into a form object.
 * 
 * <p>Example
 * {@code
 *   var poll : FvCheckPollField = new FvCheckPollField( "Your hobbies" );
 *   poll.addChoice( "basket-ball" );
 *   poll.addChoice( "football" );
 *   poll.addChoice( "actionscript" ); *   poll.addChoice( "nothing...too tired" );
 * }
 *  
 * @author Romain Ecarnot
 */
class fvaswing.components.forms.FvCheckPollField extends FvFormField
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _minSelection : Number;
	private var _list : IndexedArray;
	private var _rows : Number;
	private var _columns : Number;
	
	private var _listContainer : JPanel;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param label Field's caption
	 * @param minSelection ( optional ) Minimum choice to be selected in order 
	 * to validate the field.
	 */
	public function FvCheckPollField( label : String, minSelection : Number )
	{
		super( label );
		
		_minSelection = ( minSelection != undefined ) ? minSelection : 0;
		_isValidationEnabled = _minSelection > 0;
		
		_list = new IndexedArray();
	}
	
	/**
	 * Defines row and column value for poll choices representation.
	 * 
	 * <p>If not defined, uses row == choices list length and column == 1.
	 */
	public function setRowAndColumn( rows : Number, columns : Number ) : Void
	{
		if( rows >= 0 ) _rows = rows;
		if( columns >= 0 ) _columns = columns;
	}
	
	/**
	 * Sets selected field value.
	 */
	public function setValue( values : Array ) : Void
	{
		super.setValue( values );
	}
	
	/**
	 * Returns selected field values list.
	 */
	public function getValue( ) : Array
	{
		var l : Number = _listContainer.getComponentCount();
		var result : Array = new Array();
		
		for (var i : Number = 0; i < l; i++) 
		{
			var check : JCheckBox = JCheckBox( _listContainer.getComponent( i ) );
			if( check.isSelected() ) result.push( check.getText() );
		}
		
		return result;
	}
	
	/**
	 * Adds new choice to poll.
	 * 
	 * <p>Choices must be added to field before adding field into a form object.
	 * 
	 * @param caption Choice's label
	 */
	public function addChoice( caption : String ) : Void
	{
		_list.push( _createCheckBox( caption ) );
	}
	
	/**
	 * Clears field value.
	 */
	public function clear() : Void
	{
		var l : Number = _listContainer.getComponentCount();
		
		for (var i : Number = 0; i < l; i++) 
		{
			var check : JCheckBox = JCheckBox( _listContainer.getComponent( i ) );
			check.setSelected( false );
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Returns component dedicated to field value.
	 */
	private function _createValueComponent() : Component
	{
		var l : Number = _list.length;
		var r : Number = ( _rows != undefined ) ? _rows : l;
		var c : Number = ( _columns != undefined ) ? _columns : 1;
		
		_listContainer = new JPanel( new GridLayout( r, c, 5, 5 ) );
		
		for (var i : Number = 0; i < l; i++) _listContainer.append( _list[ i ] );
		
		return _listContainer;
	}
	
	private function _createCheckBox( caption : String ) : JCheckBox
	{
		var check = new JCheckBox( caption );
		check.setHorizontalAlignment( JCheckBox.LEFT );
		check.setHorizontalTextPosition( JCheckBox.RIGHT );
		
		return check;
	}
	
	private function _initWithData( values : Array ) : Void
	{
		clear();
		
		var n : Number = _listContainer.getComponentCount();
		var l : Number = values.length;
		
		for( var j : Number = 0; j < l; j++ )
		{
			for ( var i : Number = 0; i < n; i++ ) 
			{
				var check : JCheckBox = JCheckBox( _listContainer.getComponent( i ) );
				if( check.getText() == values[j] ) check.setSelected( true );
			}
		}
	}
	
	/**
	 * Process validation
	 */
	private function _validateField() : Boolean
	{
		var result : Array = getValue();
		
		return _triggerResult( 
			result.length >= _minSelection, 
			StringUtil.substitute( ValidationResources.getInstance().range_minimum, _minSelection ) 
		);
	}
}